package com.example.getfromservice.Category;


import java.util.ArrayList;
import java.util.List;
import com.example.getfromservice.DB.MySQLiteHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;



public class CategoryDAO {
	
	
	  private SQLiteDatabase database;
	  private MySQLiteHelper dbHelper;
	  private ContentValues values;
	  private Cursor cursor;
	  private Category category;
	  private List<Category> forms_list;
	  private String[] allColumns = {MySQLiteHelper.CAT_COLUMN_ID, MySQLiteHelper.CAT_COLUMN_CAT_ID, MySQLiteHelper.CAT_COLUMN_NAME , MySQLiteHelper.CAT_COLUMN_CHANGES };
	  
	  
	  public CategoryDAO( Context context ) {
		  
	    dbHelper = new MySQLiteHelper(context);
	    
	  }

	  public void open() throws SQLException {
		  
	    database = dbHelper.getWritableDatabase();
	    
	    Log.i("DAOCATEGORY","OPEN()");
	    
	  }

	  public void close() {
		  
	    dbHelper.close();
	    
	    Log.i("DAOCATEGORY","CLOSE()");
	    
	  }
	  
	  public Category selectCategoryfromDB( int category_id )
	  {
		 Log.i("SELECT FROM CATEGORY WHERE ", "ID = "+category_id);
		 
		 
		 cursor = database.query( MySQLiteHelper.TABLE_CATEGORY, allColumns, MySQLiteHelper.CAT_COLUMN_CAT_ID +"=" +category_id, null, null, null, null);
			
		 
			if ( cursor != null)
				{
				
		        	cursor.moveToFirst();
				}
			
	    category = new Category();
	   
	    if(doesCategoryexist(cursor.getInt(1)))
	    	{
	    		category = cursorToCategory(cursor);
	    	}

		return category; 
	
	  }

	
	  public Category createCategory( Category newcategory ){
	     
		values = new ContentValues();

	    values.put( MySQLiteHelper.CAT_COLUMN_CAT_ID     , newcategory.getID()     );
	    values.put( MySQLiteHelper.CAT_COLUMN_NAME   , newcategory.getName()   );
	    values.put( MySQLiteHelper.CAT_COLUMN_CHANGES, newcategory.getChanges());
	    
	    if(doesCategoryexist(newcategory.getID()) == false)
	    	{
	    	long insertId = database.insert(MySQLiteHelper.TABLE_CATEGORY, null,values);
	 	   
		    Log.i("KEY",insertId+"");
		    
		    cursor = database.query(MySQLiteHelper.TABLE_CATEGORY,allColumns, MySQLiteHelper.CAT_COLUMN_ID + " = " + insertId, null,null, null, null);
		
		  
		    cursor.moveToFirst();
		    
		   
		    category = cursorToCategory(cursor);
		    cursor.close();
		    
		    Log.i("DAO","CREATE CATEGORY = "+category.getID()+" "+category.getName());
			
		    
		   
	    	}
	   
	    

	    return category;
	    
	  }
	  
	  
	  public void updateCategoryinDB( Category category ){
			
			values = new ContentValues();
		
			values.put( MySQLiteHelper.CAT_COLUMN_NAME , category.getName());
		    values.put( MySQLiteHelper.CAT_COLUMN_CAT_ID , category.getID());
		    values.put( MySQLiteHelper.CAT_COLUMN_CHANGES , category.getChanges());
		 			
		    // Updating 
		   int number_row = database.update(MySQLiteHelper.TABLE_CATEGORY, values,  MySQLiteHelper.CAT_COLUMN_ID +"=?" , new String[] { String.valueOf(category.getIdDB()) });
		   
		   Log.i("UPDATE","Row = " +number_row);
		}

	
	  public void deleteCategory( Category category ) {
	   
	    
	    database.delete(MySQLiteHelper.TABLE_CATEGORY, MySQLiteHelper.CAT_COLUMN_CAT_ID + " = " + category.getID(), null);
	    database.delete(MySQLiteHelper.TABLE_BOOK, MySQLiteHelper.BOOK_COLUMN_CAT_ID + " = " + category.getID(), null);
	    
	    Log.i("DAO","DeleteCategoryinDB = "+category.getID()+" "+category.getName());
		
	  }

	  public List<Category> getAllCategory() {
		  
	    forms_list = new ArrayList<Category>();

	    cursor = database.query(MySQLiteHelper.TABLE_CATEGORY, allColumns, MySQLiteHelper.CAT_COLUMN_CHANGES +"!=?",new String[] { String.valueOf(3) }, null, null, null, null);
		
	    cursor.moveToFirst();
	    
	    while( !cursor.isAfterLast() ) {
	      
	    	category = cursorToCategory(cursor);
	    	forms_list.add(category);
	    	cursor.moveToNext();
	    }
	    
	    // Make sure to close the cursor
	    cursor.close();
	    
	    Log.i("DAO","getAllCategory size = "+forms_list.size());
		
	    
	    return forms_list;
	  }
	  
	  
	  public List<Category> getAllCategorytoSync() {
		  
		    forms_list = new ArrayList<Category>();
		    
		    //Changes in DB 0 - nothing ; 1 - update; 2 - new ;  3- delete
		    cursor = database.query(MySQLiteHelper.TABLE_CATEGORY, allColumns,null, null, null, null, null);
			
		    cursor.moveToFirst();
		    
		    while( !cursor.isAfterLast() ) {
		    	
		      category = cursorToCategory(cursor);
		      forms_list.add(category);
		      cursor.moveToNext();
		      
		    }
		    // Make sure to close the cursor
		    cursor.close();
		
		    Log.i("DAO","getAllCategory size = "+forms_list.size());
		    
		    return forms_list;
		  }
	  
	  
	  public List<Category> getAllCategoryParam( int changes ) {
		  
		    forms_list = new ArrayList<Category>();
		    
		    //Changes in DB 0 - nothing ; 1 - update; 2 - new ;  3- delete
		    cursor = database.query(MySQLiteHelper.TABLE_CATEGORY, allColumns, MySQLiteHelper.CAT_COLUMN_CHANGES +"=?",new String[] { String.valueOf(changes) }, null, null, null, null);
			
		    cursor.moveToFirst();
		    
		    while( !cursor.isAfterLast() ) {
		    	
		      category = cursorToCategory(cursor);
		      forms_list.add(category);
		      cursor.moveToNext();
		      
		    }
		    // Make sure to close the cursor
		    cursor.close();
		
		    Log.i("DAO","getAllCategory size = "+forms_list.size());
		    
		    return forms_list;
		  }
		
	  
	  public boolean doesCategoryexist( int category_id )
		  {
		
			cursor = database.query(MySQLiteHelper.TABLE_CATEGORY, allColumns, MySQLiteHelper.CAT_COLUMN_CAT_ID +"="+category_id, null, null, null, null);
			cursor.moveToFirst();
			if(cursor.getCount() == 0 ||  cursor.getInt(3) == 3 ) 
			 	{ 
				    cursor.close();
					return false;
			    }
			 else 
			 	{
				    
				 	return true;
			 	}
			 
		 

		  }
		
		public int deleteAllCategory(){
		
			int number_del_row = database.delete(MySQLiteHelper.TABLE_CATEGORY,null,null);
			
			return number_del_row;
		}
	  
	  
		
		private Category cursorToCategory(Cursor cursor) {
			 
			
			category = new Category();
			
			category.setIdDB(cursor.getInt(0));
			
		    category.setID(cursor.getInt(1));
		   
		    category.setName(cursor.getString(2));
		   
		    category.setChanges(cursor.getInt(3));
		   
		    return category;
		  }
		
		
		public int getLastInsertId() {
			
			Cursor cursor = database.query(MySQLiteHelper.TABLE_CATEGORY, allColumns,null, null, null, null, null);
			cursor.moveToLast();
			
			int last_id = (int)cursor.getLong(1);
			
			return last_id;
		}
	  
		

}
